from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect

# Create your views here.
from django.urls import reverse

from CartApp.models import AxfCart
from CartApp.view_helper import get_total_price
from MarketApp.models import AxfGoods
from UserApp.models import AxfUser


def cart(request):
    # 购物车中的数据应该是当前登录用户添加的数据
    user_id = request.session.get('user_id')
    # 若没有登录，则不允许进入购物车
    if user_id:
        user = AxfUser.objects.get(pk=user_id)

        # cart = AxfCart.objects.filter(c_user_id=user_id)   # 这个也可以获取到数据
        cart = AxfCart.objects.filter(c_user=user)

        # 判断用户刚进入购物车的时候是否是全选
        # AxfUser.objects.filter(c_user=user).filter(c_is_select=False).exists()
        # 如果上面这行代码返回true，即有商品未被选中，用not取反，即全部选中
        is_all_select = not AxfCart.objects.filter(c_user=user).filter(c_is_select=False).exists()  # 未被选择是否存在,然后取反
        # print(is_all_select)  True 或 False
        context = {
            'carts': cart,
            'is_all_select': is_all_select,
            'total_price': get_total_price(user_id)
        }

        return render(request, 'AXF/main/cart/cart.html', context=context)
    else:
        return redirect(reverse('axfuser:login'))

# 闪购中的+号
def addToCart(request):
    data = {
        'status': 200,
        'msg': 'ok',
    }

    user_id = request.session.get('user_id')
    if user_id:
        user = AxfUser.objects.get(pk=user_id)
        goodsid = request.GET.get('goodsid')
        goods = AxfGoods.objects.get(pk=goodsid)

        carts = AxfCart.objects.filter(c_user=user).filter(c_goods=goods)

        if carts.count() > 0:
            cart = carts.first()
            cart.c_goods_num = cart.c_goods_num + 1
        else:
            cart = AxfCart()
            cart.c_user = user
            cart.c_goods = goods
        cart.save()

        data['c_good_num'] = cart.c_goods_num

        return JsonResponse(data=data)

    else:
        data['status'] = 201
        data['msg'] = '用户未登陆'
        return JsonResponse(data=data)


# 闪购中的-号
def minusToCart(request):
    data = {
        'status': 200,
        'msg': '商品减1',
    }

    user_id = request.session.get('user_id')
    if user_id:
        user = AxfUser.objects.get(pk=user_id)
        goodsid = request.GET.get('goodsid')
        goods = AxfGoods.objects.get(pk=goodsid)

        carts = AxfCart.objects.filter(c_user=user).filter(c_goods=goods)

        if carts.count() > 0:
            cart = carts.first()
            if cart.c_goods_num > 0:
                cart.c_goods_num = cart.c_goods_num - 1

        cart.save()
        data['c_good_num'] = cart.c_goods_num

        return JsonResponse(data=data)

    else:
        data['status'] = 201
        data['msg'] = '用户未登陆'
        return JsonResponse(data=data)


# 购物车中的 - 号
def subToCart(request):
    data = {
        'status': 200,
        'msg': '商品数量减1',

    }
    cartid = request.GET.get('cartid')
    cart = AxfCart.objects.get(pk=cartid)  # <QuerySet [<AxfCart: AxfCart object>]>

    if cart.c_goods_num > 1:
        cart.c_goods_num = cart.c_goods_num - 1
        cart.save()
        data['c_good_num'] = cart.c_goods_num

    else:
        cart.delete()
        data['c_good_num'] = 0
        data['status'] = 201

    user_id = request.session.get('user_id')
    data['total_price'] = get_total_price(user_id)

    return JsonResponse(data=data)

# 单选
def changeStatus(request):
    data = {
        'status': 200,
        'msg': 'ok',
    }
    cartid = request.GET.get('cartid')

    cart = AxfCart.objects.get(pk=cartid)

    # 取反 true --> false; false --> true
    cart.c_is_select = not cart.c_is_select

    cart.save()
    # 返回
    data['c_is_select'] = cart.c_is_select

    user_id = request.session.get('user_id')
    is_all_select = not AxfCart.objects.filter(c_user_id=user_id).filter(c_is_select=False).exists()
    data['is_all_select'] = is_all_select
    data['total_price'] = get_total_price(user_id)

    return JsonResponse(data=data)

# 全选和全不选
def allSelect(request):
    data = {
        'status': 200,
        'msg': 'ok',
    }
    # ajax 不能发送列表数据，所有要将列表其拼接成字符串传递过来，再切割成列表
    cartlist = request.GET.get('cartlist')     # 23#32#34
    # 切割
    cart_list = cartlist.split('#')
    # print(cart_list) #['23', '32', '34']
    # 根据cart_list中传递过来的id，使用id__in方法查找所有的购物车中商品对象
    carts = AxfCart.objects.filter(id__in=cart_list)

    for cart in carts:
        cart.c_is_select = not cart.c_is_select
        cart.save()

    user_id = request.session.get('user_id')
    data['total_price'] = get_total_price(user_id)

    return JsonResponse(data=data)


# 购物车中的+号
def plusToCart(request):
    data = {
        'status': 200,
        'msg': 'ok',
    }
    cartid = request.GET.get('cartid')
    cart = AxfCart.objects.get(pk=cartid)

    if cart.c_goods_num >= 0:
        cart.c_goods_num = cart.c_goods_num + 1
        cart.save()
        data['c_good_num'] = cart.c_goods_num

    user_id = request.session.get('user_id')
    data['total_price'] = get_total_price(user_id)

    return JsonResponse(data=data)